package com.hp.team.bookstore.web.order;

import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.hp.team.bookstore.model.order.PORequirement;
import com.hp.team.bookstore.model.order.POStatus;
import com.hp.team.bookstore.model.order.PurchaseOrder;
import com.hp.team.bookstore.model.profile.User;
import com.hp.team.bookstore.service.exception.BadOperationException;
import com.hp.team.bookstore.service.order.PurchaseOrderService;
import com.hp.team.bookstore.web.IPageUrlConstants;

@Controller
@SessionAttributes(value = { "orders", "ordersAll", "title" })
public class PurchaseOrderController implements IPageUrlConstants {

	@Autowired
	public PurchaseOrderService purchaseOrderService;

	@RequestMapping(value = "/po_list")
	public String listAllOrders(@RequestParam String role, @RequestParam String operation, @RequestParam String title, ModelMap model) throws BadOperationException {

		// TODO: Check the passed role against role of the current user - the
		// current user should be saved in the session

		Map<PurchaseOrder, Set<POStatus>> ordersMap = purchaseOrderService.getOrdersMap(role, operation);

		model.addAttribute("orders", ordersMap);
		model.addAttribute("ordersAll", ordersMap);
		model.addAttribute("title", title);
		return PAGE_PURCHASE_ORDER_LIST;
	}
	
	@RequestMapping(value = "/po_list_back_all")
	public String listAllOrders(ModelMap model, @ModelAttribute("ordersAll") Map<PurchaseOrder, Set<POStatus>> orders) throws BadOperationException {
		model.addAttribute("orders", orders);
		return PAGE_PURCHASE_ORDER_LIST;
	}

	@RequestMapping(value = "/po_list_back")
	public String listOrders(ModelMap model, @ModelAttribute("orders") Map<PurchaseOrder, Set<POStatus>> orders) throws BadOperationException {
		model.addAttribute("orders", orders);
		return PAGE_PURCHASE_ORDER_LIST;
	}

	@RequestMapping(value = "/po_details")
	public String showOrderDetails(ModelMap model, @ModelAttribute("orders") Map<PurchaseOrder, Set<POStatus>> orders, @RequestParam String id) {

		long orderId = Long.parseLong(id);
		PurchaseOrder order = purchaseOrderService.getFromOrdersMap(orders, orderId);

		model.addAttribute("order", order);
		model.addAttribute("toStatus", orders.get(order));
		return PAGE_PURCHASE_ORDER_DETAILS;
	}

	@RequestMapping(value = "/po_search")
	public String searchOrders(ModelMap model, @RequestParam String status, @RequestParam String creator) {

		long creatorId = "".equals(creator) ? -1 : Long.parseLong(creator);
		Map<PurchaseOrder, Set<POStatus>> ordersMap = purchaseOrderService.getOrdersMap(status, creatorId);

		model.addAttribute("orders", ordersMap);
		model.addAttribute("operation", "SEARCH");
		return PAGE_PURCHASE_ORDER_LIST;
	}

	@RequestMapping(value = "/po_status")
	public String changeStatus(ModelMap model, @ModelAttribute("orders") Map<PurchaseOrder, Set<POStatus>> orders, @RequestParam String id, @RequestParam String status, @RequestParam String toStatus) {

		long orderId = Long.parseLong(id);
		purchaseOrderService.changeStatus(orderId, status, toStatus);
		Map<PurchaseOrder, Set<POStatus>> ordersMap = purchaseOrderService.removeFromOrdersMap(orders, orderId);

		model.addAttribute("orders", ordersMap);
		return PAGE_PURCHASE_ORDER_LIST;
	}

	@RequestMapping(value = "/po_search_page")
	public String showPageSearchOrders(ModelMap model) {

		Set<User> poSpecialists = purchaseOrderService.getAllCreators();
		Set<POStatus> statuses = purchaseOrderService.getAllAllowedStatuses();

		model.addAttribute("statuses", statuses);
		model.addAttribute("creators", poSpecialists);
		return PAGE_PURCHASE_ORDER_SEARCH;
	}

	@RequestMapping(value = "/po_new")
	public String getPoRequirement(ModelMap model) {
		Set<PORequirement> poRequirements = purchaseOrderService.getPORequirements();
		model.addAttribute("PORequirements", poRequirements);
		return PAGE_PURCHASE_ORDER_NEW;
	}

	@RequestMapping(value = "/po_check_shipped")
	public String checkNewShippedPurchaseOrders(ModelMap model, @ModelAttribute("orders") Map<PurchaseOrder, Set<POStatus>> orders) {
		Map<PurchaseOrder, Set<POStatus>> ordersMap = purchaseOrderService.checkNewShippedPurchaseOrder(orders);		
		model.addAttribute("orders", ordersMap);
		return PAGE_PURCHASE_ORDER_LIST;
		
	}
}
